package com.chianghao.rockframewrok.auth.core;

import java.util.HashSet;
import java.util.Set;

import com.chianghao.rockframewrok.auth.cache.model.Resource;
import com.chianghao.rockframewrok.auth.cache.model.Role;
import com.chianghao.rockframewrok.auth.cache.model.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.chianghao.rockframewrok.auth.cache.IAuthCache;
import com.chianghao.rockframewrok.auth.enums.ResourceType;

public abstract class AbstractAuthorizing<U extends User,R extends Resource> {

    Logger log = LoggerFactory.getLogger(this.getClass());

    protected IAuthCache authcache;

    public AbstractAuthorizing(IAuthCache authcache){
        this.authcache = authcache;
    }
    
    
    public abstract  AuthResult auth(U user, R resource) ;
    
    
    public Set<Resource> getUserAllResource(U user,ResourceType type){
    	Set<Resource> resourceSet = new HashSet<Resource>();
    	Set<Role> roles = authcache.getUserRole(user);
    	for(Role role:roles) {
    		Set<Resource> temp = authcache.getRoleResouce(role);
    		if(temp != null) {
    			for(Resource r:temp) {
    				if(r.getType().equals(type)) {
    					resourceSet.add(r);	
    				}
    			}
    		}
    	}
    	return resourceSet;
    }

	public abstract  boolean checkResourceAccent(U user, R resource);
    
    
}
